<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="优惠券名称" prop="organizationName">
                <el-input v-model="queryParams.organizationName" placeholder="请输入优惠券名称" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="优惠券渠道" prop="canalFrom">
                <el-select v-model="queryParams.canalFrom" placeholder="请选择优惠券渠道" style="width:200px;">
                    <el-option v-for="item in canalFromSel" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="适用范围" prop="canalName">
                <el-input v-model="queryParams.canalName" placeholder="请输入适用范围" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                <el-button type="success" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['decor:organization:add']">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="organizationList" row-key="couponId">
            <el-table-column label="ID" :show-overflow-tooltip="true" align="center" prop="couponId" />
            <el-table-column label="优惠券名称" :show-overflow-tooltip="true" align="center" prop="couponName">
            </el-table-column>
            <el-table-column label="优惠券渠道" :show-overflow-tooltip="true" align="center" prop="couponFrom">
            </el-table-column>
            <el-table-column label="适用范围" :show-overflow-tooltip="true" align="center" prop="couponName">
            </el-table-column>
            <el-table-column label="有效日期" :show-overflow-tooltip="true" align="center" prop="contactInformation">
            </el-table-column>
            <el-table-column label="发布数量" :show-overflow-tooltip="true" align="center" prop="contactInformation">
            </el-table-column>
            <el-table-column label="领用情况" :show-overflow-tooltip="true" align="center" prop="contactInformation">
            </el-table-column>
            <el-table-column label="状态" align="center" width="100">
                <template #default="scope">
                    <el-switch v-model="scope.row.status" inline-prompt active-value="0" inactive-value="1" active-text="启用"
                        inactive-text="停用" @click="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template #default="scope">
                    <el-button link type="primary" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['decor:organization:edit']">修改</el-button>
                    <el-button link type="warning" size="small" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['decor:organization:remove']">删除</el-button>
                    <el-button link type="primary" size="small" icon="Document" @click="handleDetail(scope.row)"
                        v-hasPermi="['decor:organization:edit']">详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

    </div>
</template>

<script setup>
import { listCoupon, delCoupon,updateCoupon } from "@/api/decor/couponMGT";
const { proxy } = getCurrentInstance();
const router = useRouter();
const { organization_status } = proxy.useDict('organization_status');
const canalFromSel = ref([
    { value: 0, label: '平台' },
    { value: 1, label: '机构' },
    { value: 2, label: '材料商' }
])
// 表格数据
const organizationList = ref([{id:1}]);
// 遮罩层
const loading = ref(false);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizationName: null,
        contactInformation: null,
        // orderByColumn: 'couponId',
        // isAsc: 'desc'
    },
});

const { queryParams } = toRefs(data);

// 表格数据
function getList() {
    loading.value = true;
    listCoupon(queryParams.value).then(response => {
        organizationList.value = response.rows;
        total.value = response.total;
        loading.value = false;

    });
}

// 搜索按钮操作
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
    router.push('/couponMGT/editCoupon')
}

/** 修改按钮操作 */
function handleUpdate(row) {
    router.push('/couponMGT/editCoupon/' + 1)
}
// 状态修改
function handleStatusChange(row) {
    let text = row.status === "0" ? "启用" : "停用";
    proxy.$modal.confirm('确认要"' + text + '""' + row.roleName + '"优惠券吗?').then(function () {
        return updateCoupon(row.couponId, row.version, row.status);
    }).then(() => {
        proxy.$modal.msgSuccess(text + "成功");
    }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
    });
}
// 详情
const handleDetail = (row) => {
    router.push('/couponMGT/detailCoupon/' + 1)
}

// 删除按钮操作
function handleDelete(row) {
    const _couponIds = row.couponId;
    proxy.$modal.confirm('是否确认删除优惠券名称为"' + row.organizationName + '"的数据？').then(function () {
        return delCoupon(_couponIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}
// getList();
</script>